package cn.yunbamboos.rest.service.core;

import cn.yunbamboos.rest.service.IRestService;
import cn.yunbamboos.rest.service.IRestServiceList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class RestServiceList implements IRestServiceList {

    private static final Logger logger = LoggerFactory.getLogger(RestServiceList.class);

    private final Map<String, IRestService> service = new ConcurrentHashMap<>();

    @Override
    public void add(IRestService service) {
        this.service.put(service.url(), service);
        logger.info("rest service {}(http://ip:port{}) in: {} out: {}", service.name(), service.url(), service.in(), service.out());
    }

    @Override
    public Optional<IRestService> get(String url) {
        return Optional.ofNullable(service.get(url));
    }
}
